options(scipen=9999)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(RSocrata)
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: spatstat.geom
## spatstat.geom 3.2-7
## Loading required package: spatstat.random
## spatstat.random 3.2-2
## Loading required package: spatstat.explore
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## 
## The following object is masked from 'package:dplyr':
## 
##     collapse
## 
## spatstat.explore 3.2-5
## Loading required package: spatstat.model
## Loading required package: rpart
## spatstat.model 3.2-8
## Loading required package: spatstat.linnet
## spatstat.linnet 3.1-3
## 
## spatstat 3.0-7 
## For an introduction to spatstat, type 'beginner'
library(viridis)
## Loading required package: viridisLite
library(FNN)
library(spdep)
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine

Read shape file

shapefile <- st_read ("Neighbourhoods.geojson") 
## Reading layer `Neighbourhoods' from data source 
##   `/Users/dongyoungkim/Library/Mobile Documents/com~apple~CloudDocs/03. Transition toronto/2024_Mar_Youth/Neighbourhoods.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 158 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -79.63926 ymin: 43.581 xmax: -79.11527 ymax: 43.85546
## Geodetic CRS:  WGS 84

Explore Area Names

table(shapefile$AREA_NAME)
## 
##                       Agincourt North          Agincourt South-Malvern West 
##                                     1                                     1 
##                             Alderwood                                 Annex 
##                                     1                                     1 
##                              Avondale                     Banbury-Don Mills 
##                                     1                                     1 
##                        Bathurst Manor                        Bay-Cloverhill 
##                                     1                                     1 
##                       Bayview Village                 Bayview Woods-Steeles 
##                                     1                                     1 
##                  Bedford Park-Nortown               Beechborough-Greenbrook 
##                                     1                                     1 
##                         Bendale South                   Bendale-Glen Andrew 
##                                     1                                     1 
##                 Birchcliffe-Cliffside                           Black Creek 
##                                     1                                     1 
##                           Blake-Jones                  Briar Hill-Belgravia 
##                                     1                                     1 
##     Bridle Path-Sunnybrook-York Mills                       Broadview North 
##                                     1                                     1 
##                   Brookhaven-Amesbury       Cabbagetown-South St.James Town 
##                                     1                                     1 
##                    Caledonia-Fairbank                             Casa Loma 
##                                     1                                     1 
##                Centennial Scarborough                      Church-Wellesley 
##                                     1                                     1 
##                   Clairlea-Birchmount                          Clanton Park 
##                                     1                                     1 
##                            Cliffcrest                Corso Italia-Davenport 
##                                     1                                     1 
##                              Danforth                    Danforth East York 
##                                     1                                     1 
##                    Don Valley Village                           Dorset Park 
##                                     1                                     1 
##                    Dovercourt Village                             Downsview 
##                                     1                                     1 
##                   Downtown Yonge East                        Dufferin Grove 
##                                     1                                     1 
##                     East End-Danforth                       East L'Amoreaux 
##                                     1                                     1 
##                       East Willowdale              Edenbridge-Humber Valley 
##                                     1                                     1 
##                         Eglinton East                      Elms-Old Rexdale 
##                                     1                                     1 
##                   Englemount-Lawrence        Eringate-Centennial-West Deane 
##                                     1                                     1 
##                 Etobicoke City Centre                   Etobicoke West Mall 
##                                     1                                     1 
##                     Fenside-Parkwoods                       Flemingdon Park 
##                                     1                                     1 
##                     Forest Hill North                     Forest Hill South 
##                                     1                                     1 
##             Fort York-Liberty Village                Glenfield-Jane Heights 
##                                     1                                     1 
##             Golfdale-Cedarbrae-Woburn                     Greenwood-Coxwell 
##                                     1                                     1 
##                             Guildwood                Harbourfront-CityPlace 
##                                     1                                     1 
##                            Henry Farm                       High Park North 
##                                     1                                     1 
##                     High Park-Swansea                        Highland Creek 
##                                     1                                     1 
##                     Hillcrest Village                     Humber Bay Shores 
##                                     1                                     1 
##              Humber Heights-Westmount                         Humber Summit 
##                                     1                                     1 
##                            Humbermede                    Humewood-Cedarvale 
##                                     1                                     1 
##                               Ionview                             Islington 
##                                     1                                     1 
##                         Junction Area              Junction-Wallace Emerson 
##                                     1                                     1 
##              Keelesdale-Eglinton West                          Kennedy Park 
##                                     1                                     1 
##                  Kensington-Chinatown         Kingsview Village-The Westway 
##                                     1                                     1 
##                        Kingsway South                       L'Amoreaux West 
##                                     1                                     1 
##                    Lambton Baby Point                      Lansing-Westgate 
##                                     1                                     1 
##                   Lawrence Park North                   Lawrence Park South 
##                                     1                                     1 
##                    Leaside-Bennington                       Little Portugal 
##                                     1                                     1 
##                           Long Branch                          Malvern East 
##                                     1                                     1 
##                          Malvern West                            Maple Leaf 
##                                     1                                     1 
##                         Markland Wood                              Milliken 
##                                     1                                     1 
##                      Mimico-Queensway                           Morningside 
##                                     1                                     1 
##                   Morningside Heights                             Moss Park 
##                                     1                                     1 
##                          Mount Dennis     Mount Olive-Silverstone-Jamestown 
##                                     1                                     1 
##                   Mount Pleasant East                           New Toronto 
##                                     1                                     1 
##                      Newtonbrook East                      Newtonbrook West 
##                                     1                                     1 
##                       North Riverdale                   North St.James Town 
##                                     1                                     1 
##                         North Toronto                     O'Connor-Parkview 
##                                     1                                     1 
##              Oakdale-Beverley Heights                              Oakridge 
##                                     1                                     1 
##                       Oakwood Village                         Old East York 
##                                     1                                     1 
##               Palmerston-Little Italy              Parkwoods-O'Connor Hills 
##                                     1                                     1 
##                  Pelmo Park-Humberlea              Playter Estates-Danforth 
##                                     1                                     1 
##                         Pleasant View                    Princess-Rosethorn 
##                                     1                                     1 
##                           Regent Park                       Rexdale-Kipling 
##                                     1                                     1 
##                     Rockcliffe-Smythe                          Roncesvalles 
##                                     1                                     1 
##                   Rosedale-Moore Park          Runnymede-Bloor West Village 
##                                     1                                     1 
##                                Rustic                   Scarborough Village 
##                                     1                                     1 
##             South Eglinton-Davisville                        South Parkdale 
##                                     1                                     1 
##                       South Riverdale St Lawrence-East Bayfront-The Islands 
##                                     1                                     1 
##                  St.Andrew-Windfields                               Steeles 
##                                     1                                     1 
##                   Stonegate-Queensway                Tam O'Shanter-Sullivan 
##                                     1                                     1 
##                         Taylor-Massey                           The Beaches 
##                                     1                                     1 
##          Thistletown-Beaumond Heights                      Thorncliffe Park 
##                                     1                                     1 
##                     Trinity-Bellwoods                            University 
##                                     1                                     1 
##                      Victoria Village                      Wellington Place 
##                                     1                                     1 
##                             West Hill                West Humber-Clairville 
##                                     1                                     1 
##                       West Queen West                            West Rouge 
##                                     1                                     1 
##                   Westminster-Branson                                Weston 
##                                     1                                     1 
##                    Weston-Pelham Park                      Wexford/Maryvale 
##                                     1                                     1 
##                       Willowdale West      Willowridge-Martingrove-Richview 
##                                     1                                     1 
##                          Woburn North                     Woodbine Corridor 
##                                     1                                     1 
##                      Woodbine-Lumsden                              Wychwood 
##                                     1                                     1 
##                    Yonge-Bay Corridor                           Yonge-Doris 
##                                     1                                     1 
##                        Yonge-Eglinton                        Yonge-St.Clair 
##                                     1                                     1 
##               York University Heights                    Yorkdale-Glen Park 
##                                     1                                     1

Identify the area of interest (Rexdale)

Explore using interactive viz

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
shapefile_with_area_name <- ggplot(data = shapefile) +
  geom_sf() + 
  geom_sf_text(aes(label = AREA_NAME), size = 3, check_overlap = TRUE) + 
  theme_minimal() 
plotly_object <- ggplotly(shapefile_with_area_name)
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
plotly_object

Air Conditioned and Cool Spaces (Heat Relief Network)

air_cool <- st_read("Air Conditioned and Cool Spaces - 4326.geojson")
## Reading layer `Air Conditioned and Cool Spaces - 4326' from data source 
##   `/Users/dongyoungkim/Library/Mobile Documents/com~apple~CloudDocs/03. Transition toronto/2024_Mar_Youth/Air Conditioned and Cool Spaces - 4326.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 822 features and 28 fields
## Geometry type: MULTIPOINT
## Dimension:     XY
## Bounding box:  xmin: -79.62265 ymin: 43.58564 xmax: -79.12511 ymax: 43.82965
## Geodetic CRS:  WGS 84
ggplot() +
  geom_sf(data = shapefile) +
  geom_sf(data = air_cool, size = 0.3, col = "blue") +
  labs(title = "Air Conditioned and Cool Spaces (Heat Relief Network)") +
  theme_classic()

Filter Rexdale AREA

rexdale <- shapefile %>% filter(AREA_NAME %in% c("West Humber-Clairville", "Mount Olive-Silverstone-Jamestown", "Thistletown-Beaumond Heights", "Rexdale-Kipling", "Elms-Old Rexdale"))
air_cool_in_rexdale <- st_intersection(air_cool, rexdale)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
ggplot() +
  geom_sf(data = rexdale) +
  geom_sf(data = air_cool_in_rexdale, size = 0.3, col = "blue") +
  labs(title = "Air Conditioned and Cool Spaces (Heat Relief Network) in Rexdale") +
  theme_classic()

Topographic Mapping – Physical location of trees

physical_location_of_trees <- st_read("TOPO_TREE_WGS84.shx")
## Reading layer `TOPO_TREE_WGS84' from data source 
##   `/Users/dongyoungkim/Library/Mobile Documents/com~apple~CloudDocs/03. Transition toronto/2024_Mar_Youth/TOPO_TREE_WGS84.shx' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1249697 features and 0 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -79.63871 ymin: 43.58133 xmax: -79.11789 ymax: 43.8552
## CRS:           NA
physical_location_of_trees_crs <- st_set_crs(physical_location_of_trees, 4326)

Too many treas to visualize (Even opacity applied)

ggplot() +
  geom_sf(data = shapefile) +
  geom_sf(data = physical_location_of_trees_crs, size = 0.3, alpha = 0.1, col = "green") +
  labs(title = "Topographic Mapping – Physical location of trees") +
  theme_classic()

library(ggplot2)
library(sf)
library(viridis) # For more color options

# Convert your SF object to a dataframe for ggplot2, extracting longitudes and latitudes
tree_data <- as.data.frame(st_coordinates(physical_location_of_trees_crs))

ggplot() +
  stat_density_2d(data = tree_data, aes(x = X, y = Y, fill = ..level..), geom = "polygon", alpha = 0.5, color = "white", size = 0.01) +
  scale_fill_viridis_c(option = "C", direction = -1) + # Density color scale
  labs(title = "Density Estimation",
       x = "Longitude",
       y = "Latitude",
       fill = "Density") +
  theme_classic() +
  coord_fixed() 
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(level)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.